<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/6/16 14:18
 */

namespace App\Transformers\Reservation;

use App\Models\Reservation\Reservation;
use App\Transformers\NoTransformer;
use Carbon\Carbon;
use League\Fractal\TransformerAbstract;

class ReservationTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['patient', 'doctor'];

    /**
     * @param \App\Models\Reservation\Reservation $reservation
     * @return array
     */
    public function transform(Reservation $reservation)
    {
        $time = Carbon::today();

        return [
            'id'         => $reservation->id,
            'doctor_id'  => $reservation->doctor_id,
            'patient_id' => $reservation->patient_id,
            'time'       => $reservation->time,
            'am'         => ['key' => $reservation->am, 'value' => $reservation->am ? '上午' : '下午'],
            'section'    => $time->hour($reservation->start)->format('H:i').'-'.$time->hour($reservation->end)->format('H:i'),
            'room'       => $reservation->room,
            'purpose'    => $reservation->purpose,
            'remark'     => $reservation->remark,
            'status'     => $reservation->status,
            'created_at' => $reservation->created_at->toDateTimeString(),
            'updated_at' => $reservation->updated_at->toDateTimeString(),
        ];
    }

    /**
     * @param \App\Models\Reservation\Reservation $reservation
     * @return mixed
     */
    public function includeDoctor(Reservation $reservation)
    {
        return $this->item($reservation->doctor, new NoTransformer());
    }

    /**
     * @param \App\Models\Reservation\Reservation $reservation
     * @return \League\Fractal\Resource\Item
     */
    public function includePatient(Reservation $reservation)
    {
        return $this->item($reservation->patient, new NoTransformer());
    }
}